package com.tebiecloud.goods.server.utils;

import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import javax.swing.filechooser.FileSystemView;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;

@Slf4j
public class DrawGoodsImage {

    public static File drawImage(String sharePicUrl,BufferedImage qrCodeImage,String qrUrl,String goodsName, int price,int retailPric) throws IOException {
        String path = FileSystemView.getFileSystemView().getHomeDirectory() + File.separator + "testQrcode";
        try {
            //画布
            BufferedImage bg = new BufferedImage(375, 603, BufferedImage.TYPE_INT_RGB);
            Graphics2D g = bg.createGraphics();
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, 375, 603);// 填充整个屏幕
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);// 消除画图锯齿
            g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);	// 消除文字锯齿

            BufferedImage bgBufImage = ImageIO.read(new URL(sharePicUrl));
            g.drawImage(bgBufImage,0,0,375,375,null);

            BufferedImage newQrCodeImage = new BufferedImage(104, 104, BufferedImage.TYPE_INT_RGB);

            newQrCodeImage.getGraphics().drawImage(qrCodeImage,0,0,104,104,null);
            g.drawImage(newQrCodeImage, 251, 478, 104, 104, null);

            BufferedImage qrImage = ImageIO.read(new URL(qrUrl));
            BufferedImage newQrImage = new BufferedImage(155, 30, BufferedImage.TYPE_INT_RGB);

            newQrImage.getGraphics().drawImage(qrImage,0,0,155,30,null);
            g.drawImage(qrImage,20,553,155,30,null);

            g.setFont(new Font("Microsoft YaHei", Font.BOLD, 20));
            g.setColor(new Color(208,2,27));
            String priceStr = "￥"+Double.valueOf(price)/100;
            g.drawString(priceStr,20,478);

            g.setFont(new Font("Microsoft YaHei", Font.BOLD, 20));
            g.setColor(new Color(0,0,0));
            g.drawString(goodsName,20,431);

            g.setFont(new Font("Microsoft YaHei", Font.BOLD, 16));
            g.setColor(new Color(153,153,153));
            String retailStr = "门市价￥"+Double.valueOf(retailPric)/100;
            g.drawString("门市价￥",20,516);
            g.drawLine(20,510,106,510);
            g.dispose();
            File file = new File(path, "sharePic.png");
            ImageIO.write(bg, "png",file);
            if (file.exists() || ((file.getParentFile().exists() || file.getParentFile().mkdirs()) && file.createNewFile())) {
                return file;
            }
        }catch (Exception e){
            log.info(">>minjie"+e.getLocalizedMessage());
        }
        return null;

    }


    public static void main(String[] args) throws IOException {
        //海报图
        String sharePicUrl = "https://gumiho.oss-cn-hangzhou.aliyuncs.com/tebie/20191014/act/act_02_share.png";
        //二维码
        String qrCodeImageUrl = "https://mggwx.oss-cn-shenzhen.aliyuncs.com/tebie/shop/image/20191016/45d3ad3bedb64014b9f175ffea56aa1d.jpg";
        //长按识别
        String qrUrl = "https://gumiho.oss-cn-hangzhou.aliyuncs.com/tebie/20191014/act/qr.png";
//        DrawGoodsImage.drawImage(sharePicUrl,qrCodeImageUrl,qrUrl);
    }
}
